import PaBaseModel from '../../core/model/PaBaseModel';

function documentItemMgtCtrl($scope, $http, ngDialog, ngDialogConfirm, SPECIAL_FOLDERS) {
  'ngInject';
  $scope.type = $state.params.type;
  $scope.id = $state.params.id;
  $scope.role = $state.params.role;
  $scope.taskId = $state.params.taskId;
  $scope.referredBoxId = $state.params.referredBoxId;

  this.getImgUrl = () => {
    let categoryId = $scope.id;
    return require(`../../../assets/images/book-${categoryId}.png`);
  };

  // 二级菜单
  this.subMenus = [];
  this.supMenu = {};
  this.initDocumentMgtView = () => {
    $http
      .post(`${SERVER_CONTEXT}/api/documentBox/getDocumentByCondition`, {
        checkCategoryID: $scope.id,
      })
      .success((respData) => {
        respData.forEach((item) => {
          if (item.requireType == 'file') {
            item.stateName = 'documentItemMgt.commonDocument';
          } else if (item.requireType == 'request') {
            SPECIAL_FOLDERS.forEach((specialFolder) => {
              if (specialFolder.name == item.boxName) {
                item.stateName = specialFolder.stateName;
              }
            })
          }

          if (item.fileType == '文档盒') {
            this.supMenu = item;
          } else if (item.fileType == '文件夹') {
            this.subMenus.push(item);
          }
        });
      })
      .then(() => {
        // pick referred folder from main.html
        let selectedIndex = 0;
        if ($scope.referredBoxId !== undefined) {
          this.subMenus.forEach((item, index) => {
            if (item.boxID == $scope.referredBoxId) {
              selectedIndex = index;
            }
          })
        }

        this.selectFolder(selectedIndex);
      })
  };
  this.initDocumentMgtView();

  this.selectFolder = (index) => {
    if (!this.subMenus[0]) {
      if (this.supMenu.stateName == 'documentItemMgt.commonDocument') {
        $state.go(this.supMenu.stateName, {
          commonType: this.supMenu.boxID,
        });
      } else {
        $state.go(this.supMenu.stateName);
      }
      return;
    }
    this.subMenus.forEach((item) => {
      item.active = false;
    });

    let activeMenu = this.subMenus[index];
    activeMenu.active = true;

    $state.go(activeMenu.stateName, {
      commonType: activeMenu.stateName ==
        'documentItemMgt.commonDocument' ? activeMenu.boxID : '',
    });

  };


  // 评分标准数据
  this.evaluationStandardList = [];
  // 获得评分标准数据方法
  this.getEvaluationStandardList = () => {
    $http.get(`${SERVER_CONTEXT}/api/safeCheck/safecheck/${$scope.id}`)
      .then((resp) => {
        this.evaluationStandardList = resp.data;
        this.evaluationStandardList.forEach((item) => {
          item.value = 0;
        });
      });
  }

  // 提交打分记录
  this.submitScore = () => {
    ngDialogConfirm
      .confirm('提交内业审核记录', '提交记录后无法更改,确认提交吗?')
      .then(() => {
        let reqData = {
          scoringRecordBeanList: [],
        };
        this.evaluationStandardList.forEach((item) => {
          let record = {};
          record.taskId = $scope.taskId;
          record.checkItemId = item.id;
          record.score = item.value;
          reqData.scoringRecordBeanList.push(record);
        });
        $http.post(`${SERVER_CONTEXT}/api/securityActivities/checkProblem/batchAddScoringRecords`,
            reqData)
          .then((resp) => {
            if (resp.data.success) {
              layer.msg(resp.data.message);
              $state.go('specialCheckItemList', {
                id: $scope.taskId,
                type: 'inner',
              });
            } else {
              layer.alert(resp.data.message);
            }
          });
      });
  };

  // 重置打分记录
  this.resetScore = () => {
    this.evaluationStandardList.forEach((item) => {
      item.value = 0;
    });
  }

  if ($scope.type == 'check') {
    this.getEvaluationStandardList();
  }

}

export default documentItemMgtCtrl;